

**first import the 2016 CMP dataset 
import delimited "dataset/MPDataset_MPDS2016b.csv", varnames(1) encoding(ISO-8859-1) clear
gen lr_tavits=(per401+per402+per407+per409+per410+per505+per507+per4011+per4012+per4013)- ///
(per403+per404+per406+per412+per413+per503+per504+per506+per4123+per4124+per4131+per4132)


format %24s partyname

keep if countryname=="Albania"|countryname=="Bulgaria"|countryname=="Croatia"| ///
countryname=="Czech Republic"|countryname=="Estonia"|countryname=="Hungary"| ///
countryname=="Latvia"|countryname=="Lithuania"|countryname=="Macedonia"| countryname=="Poland"| ///
countryname=="Romania"|countryname=="Slovakia"|countryname=="Slovenia"

**create average ideology per country/election
*first create party weights based on their seats in parliament* their ideology

gen weighted_id=absseat/totseats*lr_tavits

keep countryname date partyname party partyabb absseat totseats lr_tavits

**only keep the election years that coincide with the two expert surveys we have
		*Huber and Inglehart winter of 1993; ch hill 2002 but the surveys were taken in 2003 [from the codebook]
		*for the CMP I take for each country the elections closest to the winter of 1993 and to 2003
		*Latvia and Albania has no answers in Huber and Inglehart; Croatia, Slovenia and Macedonia are just missing in H&I; Estonia is not in Chapel Hill dataset
		
keep if countryname=="Bulgaria"&date==199110|country=="Bulgaria"&date==200106| ///
		countryname=="Czech Republic"&date==199206|country=="Czech Republic"&date==200206| ///
		country=="Hungary"&date==199405|country=="Hungary"&date==200204| ///
		country=="Lithuania"&date==199210|country=="Lithuania"&date==200010| ///
		country=="Poland"&date==199309|country=="Poland"&date==200109| ///
		country=="Romania"&date==199209|country=="Romania"&date==200011| ///
		country=="Slovakia"&date==199206|country=="Slovakia"&date==200209
		
drop if absseat==0

*now manually put in the numbers from Huber and Inglehart
gen expert_survey=.
replace expert_survey=4  if country=="Bulgaria"&date==199110&partyname=="Coalition of Bulgarian Socialist Party"
replace expert_survey=6.25  if country=="Bulgaria"&date==199110&partyabb=="SDS"
replace expert_survey=2.27  if country=="Bulgaria"&date==200106&partyabb=="KzB"
replace expert_survey=7.18  if country=="Bulgaria"&date==200106&partyabb=="ODS"
replace expert_survey=5.64  if country=="Bulgaria"&date==200106&partyabb=="NDSV"
replace expert_survey=4.91  if country=="Bulgaria"&date==200106&partyabb=="DPS"


replace expert_survey=1.33  if country=="Czech Republic"&date==199206&partyabb=="LB"
replace expert_survey=2.83  if country=="Czech Republic"&date==199206&partyname=="Czechoslovak Social Democratic Party"
replace expert_survey=7.40  if country=="Czech Republic"&date==199206&partyabb=="ODA"
replace expert_survey=7.83  if country=="Czech Republic"&date==199206&partyabb=="ODS-KDS"
		**this one above is two separate parties in huber and inglehart so i just averaged the two
replace expert_survey=3.67  if country=="Czech Republic"&date==199206&partyabb=="LSU"
replace expert_survey=7.6  if country=="Czech Republic"&date==199206&partyname=="Christian and Democratic Union - Czech People's Party"
replace expert_survey=9.8  if country=="Czech Republic"&date==199206&strpos(partyabb, "SPR-RS")

replace expert_survey=.91  if country=="Czech Republic"&date==200206&strpos(partyabb, "KS")
replace expert_survey=3.33  if country=="Czech Republic"&date==200206&strpos(partyabb, "SSD")
replace expert_survey=7.08  if country=="Czech Republic"&date==200206&strpos(partyabb, "ODS")
replace expert_survey=5.82  if country=="Czech Republic"&date==200206&strpos(partyabb, "SL-US-")
		*the one above are two separate coalitions in CH 


replace expert_survey=2.14  if country=="Hungary"&date==199405&strpos(partyabb, "MSzP")
replace expert_survey=5  if country=="Hungary"&date==199405&strpos(partyabb, "FiDe")
replace expert_survey=3.71  if country=="Hungary"&date==199405&strpos(partyabb, "SzDSz")
replace expert_survey=7.71  if country=="Hungary"&date==199405&strpos(partyabb, "MDF")
replace expert_survey=7.57  if country=="Hungary"&date==199405&strpos(partyabb, "KDNP")
replace expert_survey=9.57  if country=="Hungary"&date==199405&strpos(partyabb, "FKgP")

replace expert_survey=3.39  if country=="Hungary"&date==200204&strpos(partyabb, "MSzP")
replace expert_survey=7.64  if country=="Hungary"&date==200204&strpos(partyabb, "FiDeS")
replace expert_survey=4.21  if country=="Hungary"&date==200204&strpos(partyabb, "SzDSz")
replace expert_survey=6.43  if country=="Hungary"&date==200204&strpos(partyabb, "MDF")


replace expert_survey=4  if country=="Lithuania"&date==199210&strpos(partyabb, "LDDP")
replace expert_survey=4.6  if country=="Lithuania"&date==199210&strpos(partyabb, "LSDP")
replace expert_survey=4.75  if country=="Lithuania"&date==199210&strpos(partyabb, "LCJ")
replace expert_survey=5.4  if country=="Lithuania"&date==199210&strpos(partyabb, "LKDPK")

replace expert_survey=2.4  if country=="Lithuania"&date==200010&strpos(partyabb, "BSDK")
		*average of social dem party and russian union
replace expert_survey=3.4  if country=="Lithuania"&date==200010&strpos(partyabb, "NS")
replace expert_survey=5.6  if country=="Lithuania"&date==200010&strpos(partyabb, "LCS")
replace expert_survey=7.8  if country=="Lithuania"&date==200010&strpos(partyabb, "LLS")
replace expert_survey=7.4  if country=="Lithuania"&date==200010&strpos(partyabb, "LKDP")
replace expert_survey=8  if country=="Lithuania"&date==200010&strpos(partyabb, "TS")
replace expert_survey=2  if country=="Lithuania"&date==200010&strpos(partyabb, "LVP")
replace expert_survey=3.4  if country=="Lithuania"&date==200010&strpos(partyabb, "LLRA")


replace expert_survey=2  if country=="Poland"&date==199309&strpos(partyabb, "SLD")
replace expert_survey=5  if country=="Poland"&date==199309&strpos(partyabb, "UD")
replace expert_survey=6.25  if country=="Poland"&date==199309&strpos(partyabb, "KPN")
replace expert_survey=4  if country=="Poland"&date==199309&strpos(partyabb, "PSL")

replace expert_survey=3.56  if country=="Poland"&date==200109&strpos(partyabb, "SLD-UP")
		*averaged the two
replace expert_survey=6.88  if country=="Poland"&date==200109&strpos(partyabb, "PO")
replace expert_survey=7.75  if country=="Poland"&date==200109&strpos(partyabb, "PiS")
replace expert_survey=5.5  if country=="Poland"&date==200109&strpos(partyabb, "SRP")
replace expert_survey=9.25  if country=="Poland"&date==200109&strpos(partyabb, "LPR")
replace expert_survey=4.38  if country=="Poland"&date==200109&strpos(partyabb, "PSL")


replace expert_survey=5  if country=="Romania"&date==199209&strpos(partyabb, "FSN")
replace expert_survey=6  if country=="Romania"&date==199209&strpos(partyabb, "FDSN")
replace expert_survey=5.5  if country=="Romania"&date==199209&strpos(partyabb, "CDR")
replace expert_survey=6  if country=="Romania"&date==199209&strpos(partyabb, "PUNR")
replace expert_survey=6  if country=="Romania"&date==199209&strpos(partyabb, "PRM")
replace expert_survey=6  if country=="Romania"&date==199209&strpos(partyabb, "PSM")
		
replace expert_survey=5.09  if country=="Romania"&date==200011&strpos(partyabb, "PD")
replace expert_survey=4  if country=="Romania"&date==200011&strpos(partyabb, "PDSR")
	*averaged psd and pur
replace expert_survey=6.64  if country=="Romania"&date==200011&strpos(partyabb, "PNL")
replace expert_survey=8.55  if country=="Romania"&date==200011&strpos(partyabb, "PRM")
replace expert_survey=5.82  if country=="Romania"&date==200011&strpos(partyabb, "UDMR")


replace expert_survey=5  if country=="Slovakia"&date==199206&strpos(partyabb, "SDL")
replace expert_survey=5.67  if country=="Slovakia"&date==199206&strpos(partyabb, "KDH")
replace expert_survey=4.67 if country=="Slovakia"&date==199206&strpos(partyabb, "SNS")
replace expert_survey=5  if country=="Slovakia"&date==199206&strpos(partyabb, "HZDS")
replace expert_survey=7  if country=="Slovakia"&date==199206&strpos(partyabb, "SDL")

replace expert_survey=.64  if country=="Slovakia"&date==200209&strpos(partyabb, "KSS")
replace expert_survey=3.54  if country=="Slovakia"&date==200209&strpos(partyabb, "SDL")
replace expert_survey=6.71  if country=="Slovakia"&date==200209&strpos(partyabb, "ANO")
replace expert_survey=7.64  if country=="Slovakia"&date==200209&strpos(partyabb, "KDH")
replace expert_survey=7.07  if country=="Slovakia"&date==200209&strpos(partyabb, "SDL")
replace expert_survey=4.38  if country=="Slovakia"&date==200209&strpos(partyabb, "HZDS")
replace expert_survey=6.79  if country=="Slovakia"&date==200209&strpos(partyabb, "SMK/")

gen election=.
replace election=1 if date<200000
replace election=2 if date>200000

*change the values in early elections to adjust for the fact that the Huber Inglehart
	*scale is 1 to 10, whereas the CH scale is 0 to 10
	
replace expert_survey=(10*(expert_survey-1))/9 if election==1

	**GEN EXPERT AVERAGE IDEOLOGY
gen weighted_expert=absseat/totseats*expert_survey

*create election average
by countryname date, sort: egen average_expert=sum(weighted_expert)

	*GEN TAVITS-LETKI AVERAGE IDEOLOGY

gen weighted_id=absseat/totseats*lr_tavits

*create election average
by countryname date, sort: egen average_tavits=sum(weighted_id)

drop if countryname==countryname[_n-1]&date==date[_n-1]

keep countryname date average* election

by election, sort: egen average_tot=mean(average_expert)
by election, sort: egen average_TL_all_countries=mean(average_tavits)

	**from here graph the data in excel
	sort countryname date
